package jgl.vajra.operator.widget.base;

import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import jgl.vajra.operator.widget.mvvm.ActivityLiftCycle;
import com.orhanobut.logger.Logger;

import java.lang.ref.SoftReference;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.FragmentActivity;

public abstract class BasePopupWindow <T extends ViewDataBinding> implements ActivityLiftCycle {
    protected SoftReference<FragmentActivity> activitySoftReference;
    protected View mPopRootView;
    protected T mBinding;
    protected PopupWindow mPopupWindow;

    public BasePopupWindow(FragmentActivity activity) {
        activitySoftReference = new SoftReference<FragmentActivity>(activity);

    }

    public void init() {
        if (mPopRootView != null && mPopupWindow != null) return;
        initRootView();
        initView();
        initPopupWindow();
        initData();
        initEvent();
    }

    private void initRootView() {
        if (getLayoutId() <= 0) {
            throw new NullPointerException("layout Id is null ");
        }
        mPopRootView = View.inflate(activitySoftReference.get(), getLayoutId(), null);
        mBinding = DataBindingUtil.<T>bind(mPopRootView);
    }

    public void initPopupWindow() {
        mPopupWindow = new PopupWindow(mPopRootView, getPopupWindowWidth(), getPopupWindowHeight());
        mPopupWindow.setOutsideTouchable(true);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        mPopupWindow.setBackgroundDrawable(dw);
        mPopupWindow.setFocusable(true);// 点击空白处时，隐藏掉pop窗口
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                //popupwindow消失的时候恢复成原来的透明度
                backgroundAlpha(1f);
                onDismissPopupWindow();
            }
        });
        mPopRootView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                Logger.d("-------------------------");
                return true;
            }
        });
    }

    private int getPopupWindowWidth() {
        return RelativeLayout.LayoutParams.MATCH_PARENT;
    }

    private int getPopupWindowHeight() {
        return RelativeLayout.LayoutParams.WRAP_CONTENT;
    }


    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        if (!isBackgroundAlpha()) return;
        if (activitySoftReference.get() != null) {
            WindowManager.LayoutParams lp = activitySoftReference.get().getWindow().getAttributes();
            lp.alpha = bgAlpha; //0.0-1.0
            activitySoftReference.get().getWindow().setAttributes(lp);
        }
    }


    public boolean isBackgroundAlpha() {
        return true;
    }

    public void dismiss() {
        if (mPopupWindow != null && mPopupWindow.isShowing())
            mPopupWindow.dismiss();
    }

    public void showPopupWindowAtLocation(View v) {
        if (mPopupWindow != null && mPopupWindow.isShowing()) return;
        if (mPopupWindow == null || mPopRootView == null) {
            init();
        }
        int[] location = new int[2];
        v.getLocationOnScreen(location);  //获取控件的位置坐标
        //获取自身的长宽高
        mPopRootView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        //backgroundAlpha(R.color.titleBackgroundColor);
        backgroundAlpha(0.8f);
        mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);  //显示指定控件的上方
        onShowPopupWindow();
    }

    public void showPopupWindowAsDropDown(View v) {
        if (mPopupWindow != null && mPopupWindow.isShowing()) return;
        if (mPopupWindow == null || mPopRootView == null) {
            init();
        }
        backgroundAlpha(0.8f);
        mPopupWindow.showAsDropDown(v, 0, 0);    //显示指定控件的下方
        onShowPopupWindow();

    }

    public void onShowPopupWindow() {

    }

    public void onDismissPopupWindow() {

    }

    public boolean isShowing() {
        if (mPopupWindow == null) return false;
        return mPopupWindow.isShowing();
    }

    public void onDestroy() {
        if (isShowing()) {
            mPopupWindow.dismiss();
        }
        mPopupWindow = null;
        mPopRootView = null;
        mBinding = null;
        activitySoftReference = null;
    }

    public   void onBackPressed(){

    }
}
